#import <Foundation/Foundation.h>
#import <Carbon/Carbon.h>

int main (int argc, const char * argv[]) 
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

	if (argc == 4)
	{
		NSString * scriptUrl = [NSString stringWithCString:argv[1]];
		NSString * prediction = [NSString stringWithCString:argv[2]];
		NSString * value = [NSString stringWithCString:argv[3]];
		
		NSAppleScript * scpt = [[NSAppleScript alloc] initWithContentsOfURL:[NSURL URLWithString:scriptUrl] error:nil];
		
		if (scpt != nil)
		{
			NSDictionary * errors = nil;

			// NSLog (@"executing %@", scriptUrl);
			NSAppleEventDescriptor * type = [NSAppleEventDescriptor descriptorWithString:prediction];
			NSAppleEventDescriptor * prediction = [NSAppleEventDescriptor descriptorWithString:value];
			
			NSAppleEventDescriptor * parameters = [NSAppleEventDescriptor listDescriptor];
			[parameters insertDescriptor:type atIndex:1];
			[parameters insertDescriptor:prediction atIndex:2];
			
			ProcessSerialNumber psn = { 0, kCurrentProcess };
			NSAppleEventDescriptor * target = [NSAppleEventDescriptor descriptorWithDescriptorType:typeProcessSerialNumber
																							 bytes:&psn
																							length:sizeof(ProcessSerialNumber)];
			
			NSAppleEventDescriptor *handler = [NSAppleEventDescriptor descriptorWithString:@"prediction"];
			
			NSAppleEventDescriptor *event = [NSAppleEventDescriptor appleEventWithEventClass:kASAppleScriptSuite
																					 eventID:kASSubroutineEvent
																			targetDescriptor:target
																					returnID:kAutoGenerateReturnID
																			   transactionID:kAnyTransactionID];
			
			[event setParamDescriptor:handler forKeyword:keyASSubroutineName];
			[event setParamDescriptor:parameters forKeyword:keyDirectObject];
			
			[scpt executeAppleEvent:event error:&errors];
			[scpt release];
		}
		
	}
	
    [pool drain];
    return 0;
}
